#include <stdio.h>
#include <stdlib.h>

#define ERROR -1
typedef struct TreeNode *Tree;
struct TreeNode {
    int   Key;
    Tree  Left;
    Tree  Right;
};
Tree Insert( Tree T, int X );
int LCA ( Tree T,  int u, int v );

int main()
{
    Tree T;
    int u, v, ans;
    int n,i,X;
	T=NULL;
	scanf("%d",&n);
    for ( i=0; i<n; i++ ) {
        scanf("%d", &X);
        T = Insert(T, X);
    }
    scanf("%d %d", &u, &v);
    ans = LCA(T, u, v);
    if ( ans == ERROR ) printf("Wrong input\n");
    else printf("LCA = %d\n", ans);

    return 0;
}
Tree Insert( Tree T, int X )
{
	if(T==NULL) 
	{
		T = (Tree) malloc(sizeof(struct TreeNode));
		T->Key=X;
		T->Left=T->Right=NULL;
		
	}
	else if(X<T->Key)
	T->Left=Insert(T->Left,X);
	else 
	T->Right=Insert(T->Right,X);
	return T;
} 
int LCA( Tree T,  int u, int v )
{
	Tree p;
	int flag1=0,flag2=0;
	p=T;
	while(p)
	{
		if(p->Key==u)
	{
			flag1=1;
			break;}
		if(p->Key>u)
		p=p->Left;
		else
		p=p->Right;
		
	}
	p=T;
	while(p)
	{
		if(p->Key==v)
		{flag2=1;
		break;
		}
		if(p->Key>v)
		p=p->Left;
		else
		p=p->Right;
		
	}
	if(!T)
	return ERROR;	
	while(T!=NULL)
	{
		if(flag1==0||flag2==0)
		return ERROR;
		if(u==T->Key||v==T->Key)
		return T->Key;
		if(u>T->Key&&v<T->Key||u<T->Key&&v>T->Key)
		return T->Key;
		if(u>T->Key)
		T=T->Right;
		if(u<T->Key)
		T=T->Left;
	}
}
